package com.exercise.AndroidCamera;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;
import android.widget.ZoomControls;
/*
기능 : 카메라 기능 구현
작성 : 하드캔
완료일 : 2014.09.26 
	*/
public class AndroidCamera extends FragmentActivity implements SurfaceHolder.Callback, SensorEventListener {

	Intent mIntent;
	String userID;
	
	Camera camera;
	SurfaceView surfaceView;
	SurfaceHolder surfaceHolder;
	boolean previewing = false;
	LayoutInflater controlInflater = null;
	
    SensorManager m_sensor_manager;
    Sensor m_ot_sensor;

    TextView m_result_view[] = new TextView[3];

    int currentZoomLevel = 0, maxZoomLevel = 0;

    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 //       setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        
		userID = getIntent().getExtras().getString("userID");
        
        m_result_view[0] = (TextView) findViewById(R.id.textView2);
        m_result_view[1] = (TextView) findViewById(R.id.textView3);
        m_result_view[2] = (TextView) findViewById(R.id.textView4);
        
        m_sensor_manager = (SensorManager)getSystemService(SENSOR_SERVICE);
        m_ot_sensor = m_sensor_manager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
        
        getWindow().setFormat(PixelFormat.UNKNOWN);
        surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        
        controlInflater = LayoutInflater.from(getBaseContext());
        View viewControl = controlInflater.inflate(R.layout.activity_camera, null);
        LayoutParams layoutParamsControl 
        	= new LayoutParams(LayoutParams.FILL_PARENT, 
        	LayoutParams.FILL_PARENT);
        this.addContentView(viewControl, layoutParamsControl);
        
		getActionBar().setTitle("Post Memories");
		getActionBar().setDisplayShowHomeEnabled(false);
    }
    

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub
		camera.setDisplayOrientation(90);
		if(previewing){
			camera.stopPreview();
			previewing = false;
		}
		
		if (camera != null){
			try {
				camera.setPreviewDisplay(surfaceHolder);
				camera.startPreview();
				previewing = true;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		Camera.Parameters parameters = camera.getParameters();
		camera.setParameters(parameters);
		camera.startPreview();
		
	    ZoomControls zoomControls = (ZoomControls) findViewById(R.id.CAMERA_ZOOM_CONTROLS);

	    if(parameters.isZoomSupported()){    
	    maxZoomLevel = parameters.getMaxZoom();

	    	zoomControls.setIsZoomInEnabled(true);
	        zoomControls.setIsZoomOutEnabled(true);

	        zoomControls.setOnZoomInClickListener(new View.OnClickListener(){
	            public void onClick(View v){
	            	
	                    if(currentZoomLevel < 500){
	                        currentZoomLevel++;
	                        camera.startSmoothZoom(currentZoomLevel);
	                    }
	            }
	        });

	        zoomControls.setOnZoomOutClickListener(new OnClickListener(){
	            public void onClick(View v){

	                    if(currentZoomLevel > 0){
	                        currentZoomLevel--;
	                        camera.startSmoothZoom(currentZoomLevel);
	                    }
	            }
	        });    
	   }
	   else
		   zoomControls.hide();
	}
	
	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		camera = Camera.open();
		
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		camera.stopPreview();
		camera.release();
		camera = null;
		previewing = false;
	}

    protected void onResume() {
        super.onResume();

        m_sensor_manager.registerListener(this, m_ot_sensor, SensorManager.SENSOR_DELAY_UI);
    }

    protected void onPause() {
        super.onPause();
        m_sensor_manager.unregisterListener(this);
    }

	@Override
	public void onSensorChanged(SensorEvent event) {
		// TODO Auto-generated method stub
        if(event.sensor.getType() == Sensor.TYPE_ORIENTATION) {
        	System.out.println("x : "+event.values[0]);
        	System.out.println("y : "+event.values[1]);
        	System.out.println("z : "+event.values[2]);
        }
	}


	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    getMenuInflater().inflate(R.menu.camera_menu, menu);
	    return true;
	}
	 
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	     
	    switch(item.getItemId()){
	         
	    case R.id.post:
	    	mIntent = new Intent(AndroidCamera.this, WritePostActivity.class);
	        mIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	    	mIntent.putExtra("userID", userID);
    		startActivity(mIntent);
	        break;
	         
	    case R.id.back:
	    	mIntent = new Intent(AndroidCamera.this, MainActivity.class);
	        mIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	    	mIntent.putExtra("userID", userID);
    		startActivity(mIntent);
	        break;
	         
	    default:
	        return false;
	    }
	    return true;
	}
}